package web.services;

import web.bean.District;
import web.dao.LocationDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/7/7.
 */
@Service
public class LocationService {
    @Autowired
    private LocationDao locationDao;

//    通过省份编号查名称
    private static Map<String,Object> locationMap=null;

//    通过省份名称查code
    private static Map<String,Object> locationMapByName=null;

//    查询所有省份编号与名称
    private static List<Map<String,Object>> locationList=null;

//     查询所有地域信息
    private static List<String> locationAllList=null;

//    通过省份查询所有城市
    private Map<Integer,List<District>> districtParentIdMap=null;

    private Map<Integer,Integer> locationParentMap=null;



    /**
     * 查询省份
     * @return
     */
    public synchronized Map<String,Object> getLocationMap(){
        if(locationMap==null){
            locationMap=locationDao.findLocation();
        }
        return locationMap;
    }

    /**
     * 通过省份名称查code
     * @return
     */
    public synchronized Map<String,Object> getLocationMapByName(){
        if(locationMapByName==null){
            locationMapByName=locationDao.findLocationByName();
        }
        return locationMapByName;
    }


    /**
     * 查询所有省份
     * @return
     */
    public synchronized List<Map<String,Object>> getLocationAllList(){
        if(locationList==null){
            locationList=locationDao.findAllLocation();
        }
        return locationList;
    }

    /**
     * 查询所有地域
     * @return
     */
    public synchronized List<String>getLocationList(){
        if(locationAllList==null){
            locationAllList=locationDao.findAllLocationList();
        }
        return locationAllList;
    }

    /**
     * 地域展示转换
     * @param provinceIds
     * @return
     */
    public String getUserSearchProvince(String provinceIds){
        if(provinceIds==null || provinceIds.length()==0){
            return "";
        }
        String [] str=provinceIds.trim().split(",");
        StringBuffer sbf=new StringBuffer();
        for (String s : str) {
            if(!str[str.length-1].equals(s)) {
                sbf.append(provinceStr(s) + "，");
            }else{
                sbf.append(provinceStr(s) );
            }
        }
        return sbf.toString().trim();
    }

    public String provinceStr(String province){
        String location="";
        String [] strs=province.split(" ");
        for (String str : strs) {
            location+=getLocationMap().get(str)+" ";
        }
        return location.trim();
    }


    public synchronized  Map<Integer,List<District>> getDistrictMap(){
        if(districtParentIdMap==null){
            districtParentIdMap=new HashMap<Integer, List<District>>();
            List<String> list=locationDao.findAllLocationList();
            for(String id:list){

                int parentId=getLocationParentMap().get(Integer.parseInt(id));
                districtParentIdMap.put(Integer.parseInt(id), locationDao.getLocationList1(parentId));
            }

        }
        return districtParentIdMap;
    }

    public Map<Integer,Integer> getLocationParentMap(){
        if(locationParentMap==null){
            locationParentMap=locationDao.findParent();
            new Thread(){
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000*60*60*24);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    locationParentMap=locationDao.findParent();
                }
            }.start();
        }
        return locationParentMap;
    }

    /**
     * 通过城市查到对应的省份
     * @param cityCode
     * @return
     */
    public int findProvinceByCity(int cityCode ){
       return locationDao.findProvinceByCity(cityCode);
    }

    public  Map<Integer,Integer> getAllCodeById (){
        return locationDao.getAllCodeById();
    }
    public  Map<Integer,Integer> getALLParentByCity (){
        return  locationDao.getALLParentByCity();
    }


    /**
     * 处理关键词中的省份
     * @param key
     * @return
     */
    public String[] findLocationByKey(String key){
        String [] strs=new String[]{"","0"};
        if(key.trim().split(" ").length==1){
            return strs;
        }

        String [] str=key.split(" ");

        int num = 0;
//        判断关键词中有多少个地域
        for(String s : str){
            if(getLocationMapByName().get(s)!=null){
                num++;
            }
        }
//        当总地域超过1个时，则不使用关键词中的地域
        if(num > 1){
            return strs;
        }

        for (String s : str) {
            if(getLocationMapByName().get(s)!=null){
                strs[0]=s;
                strs[1]=getLocationMapByName().get(s).toString();
                return strs;
            }
        }
        return strs;
    }

}
